#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>

//模拟ls
int myLs(const char *dirname);

//-a打印目录下的所有文件名
int myLs_a(const char *dirname);

//-l打印详细信息
int myLs_l(const char *dirname);

//打印时间函数
void printTime(struct tm *tm, char *time);

int main(int argc, const char *argv[])
{
    int num = 0;
    if(argc == 2)
    {
        num = myLs(argv[1]);
        printf("\n");
        if(!num)
        {
            printf("打印失败！\n");
        }
    }
    else if(argc == 3)
    {
        if(!strcmp(argv[2], "-a"))
        {
            num = myLs_a(argv[1]);
            printf("\n");
            if(!num)
            {
                printf("打印失败！\n");
            }
        }
        
        if(!strcmp(argv[2],"-l"))
        {
            num = myLs_l(argv[1]);
            if(!num)
            {
                printf("打印失败！\n");
            }
        }    
    }
    else
    {
        printf("输入有误！\n");
        return -1;
    }
    
    return 0;
}

//ls
int myLs(const char *dirname)
{
    DIR *dir = NULL;
    struct dirent *ptr = NULL;
    int i = 0;
    //打开目录
    dir = opendir(dirname);
    while((ptr = readdir(dir)) != NULL)
    {
        if(ptr->d_name[0] != '.')
        {
            if(i < 4) //每打印四个换行
            {
                printf("%-15s\t", ptr->d_name);
            }
            else
            {
                printf("\n");
                i = 0;
            }
            i++;
        }
    }

    closedir(dir);

    return 1;
}

//ls -a
int myLs_a(const char *dirname)
{
    DIR *dir = NULL;
    struct dirent *ptr = NULL;
    int i = 0;
    //打开目录
    dir = opendir(dirname);
    while((ptr = readdir(dir)) != NULL)
    {
        if(i < 4) //每打印四个换行
        {
            printf("%-15s\t", ptr->d_name);
        }
        else
        {
            printf("\n");
            i = 0;
        }
        i++;
    }

    closedir(dir);

    return 1;
}

//ls -l
int myLs_l(const char *dirname)
{
    DIR *dir = NULL;
    struct dirent *ptr = NULL;
    char timech[100];
    struct stat sb;
    struct passwd *pwd;
    struct group *gp;
    
    stat(dirname, &sb);
    //打开目录
    dir = opendir(dirname);
    while((ptr = readdir(dir)) != NULL)
    {
	    switch (ptr->d_type) //类型
	    {
            case DT_REG:  printf("-");          
                    break;
            case DT_DIR:  printf("d");            
                    break;
            default:       printf("other");                
                    break;
	    }

        char str[3] = {'r', 'w', 'x'};
        for(int i = 0; i < 9; i++)//权限
        {
            printf("%c", sb.st_mode & (0400>>i) ? str[i % 3] : '-'); 
        }
        printf(" %ld ", sb.st_nlink);
        printTime(localtime(&sb.st_atime), timech);//时间
        pwd = getpwuid(sb.st_uid);//所有者
        gp = getgrgid(sb.st_gid);//所有组
		printf("%s %s %ld %s %s\n", pwd->pw_name, gp->gr_name, sb.st_size, timech, ptr->d_name);
    }

    closedir(dir);

    return 1;
}

void printTime(struct tm *tm, char *time)
{
	sprintf(time, "%d月 %d %d:%d", tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min);
}

